<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>部门</title>
    <link href="${pageContext.request.contextPath}/statics/layui/layui/css/layui.css" rel="stylesheet">
</head>
<body>

<div style="width: 88%;height: 700px;float: left;display: inline-block;">
    <div style="margin-left: 50px;margin-top: 20px">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">部门名称：</label>
                    <div class="layui-input-inline">
                        <input class="layui-input" name="deptName" id="deptName" autocomplete="off">
                    </div>
                    <label class="layui-form-label">部门状态：</label>
                    <div class="layui-input-inline">
                        <input class="layui-input" name="status" id="status" autocomplete="off">
                    </div>
                </div>
                <div class="layui-btn-container layui-inline" style="margin-left: 100px;margin-top: 9px">
                    <button type="submit" lay-submit="" class="layui-btn layui-btn-primary" lay-filter="demo1">
                        <i class="layui-icon layui-icon-search"></i>
                    </button>
                </div>
            </div>
        </form>
    </div>

    <table class="layui-hide" id="test" lay-filter="test"></table>
</div>

<script src="${pageContext.request.contextPath}/statics/layui/layui/layui.js"></script>

<script type="text/html" id="statusScript">
    {{d.status==0 ? '正常' : '停用'}}
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="view-t">查看</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script>
    layui.use(['table', 'form', 'jquery', 'layer', 'tree', 'util'], function () {
        var layer = layui.layer;
        var table = layui.table;
        var form = layui.form;
        var $ = layui.$;
        var tree = layui.tree;
        var util = layui.util;

        var tableIns = table.render({
            elem: '#test'
            , url: '${pageContext.request.contextPath}/dept/list'
            , toolbar: '#toolbarDemo'
            , cols: [[
                {type: 'checkbox', title: '选择', width: 60, style: "", sort: false, align: 'center'}
                , {field: 'deptId', width: 80, title: '编号', sort: false, align: 'center'}
                , {field: 'deptName', width: 120, title: '部门名称', align: 'center'}
                , {field: 'status', width: 120, title: '状态', align: 'center', templet: '#statusScript', sort: false}
                , {field: 'leader', width: 120, title: '负责人', align: 'center'}
                , {field: 'phone', width: 120, title: '联系电话', align: 'center'}
                , {field: 'createTime', width: 120, title: '创建时间', align: 'center'}
                , {field: '', width: 180, title: '操作', align: 'center', templet: '#barDemo', sort: false}
            ]]
            , page: true
            , id: 'idTest'
        });

        form.on('switch(status)', function (obj) {
            var status = 0;
            if (obj.elem.checked == true) {
                status = 1;
            }
            layer.confirm(status == 1 ? '确认要停用用户吗？' : '确认要启用用户吗？', function (index) {
                $.getJSON('${pageContext.request.contextPath}/user/status', {
                    id: this.value,
                    status: status
                }, function (data) {
                    if (data === "true") {
                        if (status == 1) {
                            layer.msg("用户已停用", {
                                icon: 1,
                                time: 1000
                            })
                        } else {
                            layer.msg("用户已启用", {
                                icon: 1,
                                time: 1000
                            })
                        }
                    } else {
                        layer.msg("操作无效", {
                            icon: 2,
                            time: 1000
                        })
                    }
                })
                layer.close(index);
            })
        });

        //触发单元格工具事件
        table.on('tool(test)', function (obj) { // 双击 toolDouble
            var data = obj.data;
            // console.log(data.id)
            if (obj.event === 'view-t') {
                layer.msg('该功能待开发……', {
                    icon: 4,
                    time: 1000
                })
            } else if (obj.event === 'view-y') {
                $("#seeView").html("");

                $.get('see/' + data.id, function (data) {
                    console.log(data);
                    // let parse = JSON.parse(data);
                    $.each(data, function (index, value) {
                        console.log(index + ":" + value)
                        if (index === "sex") {
                            if (value == 1) {
                                value = "女"
                            } else {
                                value = "男"
                            }
                        }
                        if (index === "roleId" || index === "createdUserId" || index === "createdTime") {
                        } else {

                            if (index === "idpicpath") {
                                $("#seeView").append('<img src="${pageContext.request.contextPath}/statics/upload/' + value + '" width="50px" height="50px">     ')
                            } else {
                                $("#seeView").append(value + "     ");
                            }
                        }
                    });
                })

            } else if (obj.event === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    var layui_page = $(".layui-laypage-em").next().html();
                    // 发送删除请求
                    $.get('${pageContext.request.contextPath}/user/delete/' + data.userId, function (data) {
                        if (data === "1") {
                            layer.msg('删除成功', {
                                icon: 1,
                                time: 1000
                            }, function () {
                                window.location.reload();
                            })
                        } else {
                            layer.msg('删除失败', {
                                icon: 2,
                                time: 1000,
                                cancel: function (index, layero) {
                                    tableIns.reload({
                                        where: { //设定异步数据接口的额外参数，任意设
                                            //…
                                        }
                                        , page: {
                                            curr: layui_page //重新从第 N 页开始
                                        }
                                    });
                                    layer.close(index)
                                    return false;
                                }
                            })
                        }
                    })
                    // 关闭提示
                    layer.close(index);
                });
            } else if (obj.event === 'edit') {
                // 获取当前页码
                var layui_page = $(".layui-laypage-em").next().html();
                // window.open('update/'+data.id)
                layer.open({
                    type: 2,
                    title: '编辑',
                    area: ['650px', '480px'],
                    content: ['${pageContext.request.contextPath}/user/updates/' + data.userId, 'no'], //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
                    cancel: function (index, layero) {
                        tableIns.reload({
                            where: { //设定异步数据接口的额外参数，任意设
                                //…
                            }
                            , page: {
                                curr: layui_page //重新从第 N 页开始
                            }
                        });
                        layer.close(index)
                        return false;
                    }
                });
            }
        });

        var $ = layui.$, active = {
            getCheckData: function () { //获取选中数据
                var checkStatus = table.checkStatus('idTest')
                    , data = checkStatus.data;
                console.log(checkStatus);
                // console.log(data.length);
                // console.log(data[0].userId);

                var id = "";

                for (var i = 0; i < data.length; i++) {
                    id = id + data[i].userId + ",";
                }

                $.get('${pageContext.request.contextPath}/user/deletes/' + id, function (data) {
                    var layui_page = $(".layui-laypage-em").next().html();
                    if (data === "1") {
                        layer.msg('删除成功', {
                            icon: 1,
                            time: 1000,
                            cancel: function (index, layero) {
                                tableIns.reload({
                                    where: { //设定异步数据接口的额外参数，任意设
                                        //…
                                    }
                                    , page: {
                                        curr: layui_page //重新从第 N 页开始
                                    }
                                });
                                layer.close(index)
                                return false;
                            }
                        }, function () {
                            // 获取当前页码
                            var layui_page = $(".layui-laypage-em").next().html();
                            // 获取一页有多少条数据
                            var checkStatus = table.checkStatus('idTest'), data = checkStatus.data;

                            <%--location.href='${pageContext.request.contextPath}/user/list/page';--%>
                            window.location.reload();
                        })
                    } else {
                        layer.msg('删除失败', {
                            icon: 2,
                            time: 1000
                        })
                    }
                })

                // layer.alert(JSON.stringify(data));
            }
            , getCheckData2: function () { //获取选中数目
                var layui_page = $(".layui-laypage-em").next().html();
                console.log(layui_page);
                var checkStatus = table.checkStatus('idTest')
                    , data = checkStatus.data;
                layer.msg('选中了：' + data.length + ' 个');
            }
            , getCheckData3: function () { //验证是否全选
                var checkStatus = table.checkStatus('idTest');
                layer.msg(checkStatus.isAll ? '全选' : '未全选')
            }
        };

        $('.demoTable .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        window.refresh = function () {
            table.reload('role-table');
        }

        // 工具栏事件
        table.on('toolbar(test)', function (obj) {
            switch (obj.event) {
                case 'add':
                    var layui_page = $(".layui-laypage-em").next().html();
                    // window.open('toadd')
                    layer.open({
                        type: 2,
                        title: '新增',
                        area: ['500px', '600px'],  //框高
                        content: '${pageContext.request.contextPath}/user/addPage',
                        cancel: function (index, layero) {
                            tableIns.reload({
                                where: { //设定异步数据接口的额外参数，任意设
                                    //…
                                }
                                , page: {
                                    curr: layui_page //重新从第 N 页开始
                                }
                            });
                            layer.close(index)
                            return false;
                        }
                    });
                    break;
            }
        });

        //根据用户登录账号和手机号进行模糊查询
        form.on('submit(demo1)', function (data) {
            console.log(data.field)
            tableIns.reload({
                where: {
                    loginName: data.field.loginName
                    , phonenumber: data.field.phonenumber
                }
                , page: {}
            });
            return false;
        });

        $.get('${pageContext.request.contextPath}/dept/tree',function (data) {
            // console.log(data);
            //常规用法
            tree.render({
                elem: '#test1' //默认是点击节点可进行收缩
                , data: [data]
                ,click: function(obj){
                    console.log(obj.data); //得到当前点击的节点数据
                    console.log(obj.state); //得到当前节点的展开状态：open、close、normal
                    console.log(obj.elem); //得到当前节点元素
                    console.log(obj.data.children); //当前节点下是否有子节点

                    tableIns.reload({
                        where: {
                            deptId: obj.data.id
                        }
                        , page: {}
                    });

                }
            });
        })

    })
</script>

</body>
</html>
